package com.phone580.app.login;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.client.ClientProtocolException;
import org.xml.sax.SAXException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

import com.phone580.app.BaseTask;
import com.phone580.app.CommonData;
import com.phone580.app.entity.LoginEntity;
import com.phone580.app.model.LoginModel;
import com.phone580.app.net.HttpAccess;
import com.phone580.app.untilty.AlertUtil;

public class LoginTask extends BaseTask{
	
	private String username;
	private String pwd;
	
	public LoginTask(Context context) {
		super(context);
	}

	@Override
	protected void onPreExecute() {
		showDialog("正在登录，请稍后...", false);
	}

	
//	@Override
//	protected void onPostExecute(Boolean result) {
//		dialog.dismiss();
//		if (result && CommonData.login.isSuccess()) {
//			Toast.makeText(context, "登录成功", Toast.LENGTH_SHORT).show();
//			((Activity)context).finish();
//		} else {
//			Toast.makeText(context, CommonData.login.getMessage(), Toast.LENGTH_SHORT).show();
//		}
//	}

	@Override
	public void setParams(Object... params) {
		username = (String)params[0];
		pwd = (String)params[1];
		
	}

	@Override
	protected Object doInBackground(Void... params) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("staffCode", username);
		map.put("password", pwd);
		try {
			String result = HttpAccess.doGet(CommonData.LOGIN_URL, map);
			LoginModel model = new LoginModel();
			LoginEntity entity = model.parseLogin(result.trim());
			CommonData.login = entity;
			return entity;
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	} 

}
